package cn.ipanel.bigdata.boot.date

import cn.ipanel.bigdata.boot.period.Period

/**
 * Author: lzz
 * Date: 2021/11/19 15:34
 */
case class Day(st: Long, et: Long) extends Date(st, et) {

  override def prev(n: Int = 1): Day = Day(st - Date.DAY_TIME * n)
  override def next(n: Int = 1): Day = Day(et + Date.DAY_TIME * n)

  def isTodayBefore: Boolean = Date.asToday.st > et
  def isToday: Boolean = Date.asToday.equals(this)
  def isYesterday: Boolean = Date.asYesterday.equals(this)
  def toPeriodValue(period: Period): Long = period match {
    case Period.YEAR => this.asYear.toYear
    case Period.QUARTER => this.asQuarter.toYearQuarter
    case Period.MONTH => this.asMonth.toYearMonth
    case Period.WEEK => this.asWeek.toWeekDay
    case Period.DAY => this.asDay.toDate
    case Period.HOUR => this.asHour.toDayHour
    case Period.MINUTE => this.asMinute.toDayMinute
    case _ => 0
  }
  override def toString: String = s"Day(${STime.toYYYYMMddHHmmss} To ${ETime.toYYYYMMddHHmmss})"
}

object Day {

  def unapply(time: Long): Option[(Long, Long)] = {
    val ca: java.util.Calendar = java.util.Calendar.getInstance()
    ca.setTime(new java.util.Date(time))
    ca.set(java.util.Calendar.HOUR_OF_DAY, 0)
    ca.set(java.util.Calendar.MINUTE, 0)
    ca.set(java.util.Calendar.SECOND, 0)
    ca.set(java.util.Calendar.MILLISECOND, 0)
    Some(ca.getTimeInMillis, ca.getTimeInMillis + Date.DAY_TIME - 1)
  }

  def apply(yyyyMMdd: String): Day = apply(Time(yyyyMMdd))
  def apply(time: Time): Day = apply(time.time)
  def apply(time: Long): Day = time match {
    case Day(st, et) => Day(st, et)
    case _ => null
  }
}